package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.TreeMap;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository travelCommentRepository;

    /**
     * @param id
     * @return
     */
    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).orElse(null);
    }

    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelComment.setCreateTime(new Date());
        //判断关联的评论的id是否为空
        //是：回复
        String id = travelComment.getRefComment().getId();
        if (StringUtils.hasLength(id)) {
            TravelComment refcomment = this.get(id);
            travelComment.setRefComment(refcomment);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        } else {
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        travelCommentRepository.save(travelComment);
    }

    @Override
    public List<TravelComment> findAll() {
        return travelCommentRepository.findAll();
    }

    @Override
    public List<TravelComment> queryByTravelId(String id) {
        return travelCommentRepository.findByTravelId(id);
    }
}
